package com.gzgy.ustore.api.dao.impl;

import com.gzgy.ustore.api.common.jdbc.StatementParameter;
import com.gzgy.ustore.api.dao.PushLogDao;
import com.ustore.model.PushLog;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
public class PushLogDaoImpl  extends AbstractBaseDao<PushLog> implements PushLogDao {
    @Override
    public int pushLogCount(Integer type,String deviceToken) {
        StatementParameter parameter = new StatementParameter();
        StringBuffer sb = new StringBuffer();
        sb.append("select count(ID) from "+this.tableName);
        if( type !=null){
            sb.append(" where push_type = ? ");
            parameter.setInt(type);
            if(type ==1){
                sb.append( " and push_device = ?");
                parameter.setString(deviceToken);
            }
        }
        return this.queryForInt(sb.toString(),parameter);
    }

    @Override
    public List<PushLog> pushList(Integer type, String deviceToken,int index,int page) {
        StatementParameter parameter = new StatementParameter();
        StringBuffer sb = new StringBuffer();
        sb.append("select * from "+this.tableName);
        if( type !=null){
            sb.append(" where push_type = ? ");
            parameter.setInt(type);
            if(type ==1){
                sb.append( "and push_device = ? ");
                parameter.setString(deviceToken);
            }
        }
        sb.append(" limit ?,?");
        parameter.setInt(index*page);
        parameter.setInt(page);

        return this.queryForList(sb.toString(),PushLog.class,parameter);
    }
}
